<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Models\User;
use Illuminate\Support\Arr;

class UserController extends Controller
{
    public $arr = [];
    public function response($status = 200,$message = '',$data = null){
            return ['status'=>$status,'message'=>$message,'data'=>$data];
        }
    public function login(Request $request){
        $user = $request->input('name','');
        $pass = $request->input('password','');
        $password = DB::table('users')->where('name',"=",$user)->value('password');
        if (!empty($user)){
            if (!empty($pass)){
                if ($pass == $password){
                    $this->arr = $this->response(200,'登录成功');
                    print_r($this->arr);
                    $list = self::list();
                    header('location:/list');
                }else{
                    $this->arr = $this->response(400,'用户名或密码错误');
                }
            }else{
                $this->arr = $this->response(400,'请输入密码');
            }
        }else{
            $this->arr = $this->response(400,'请输入用户名');
        }
//        $this->index();
        print_r($this->arr);
    }

    public function index(Request $request){
        return view('index');
    }
    public function list(){
        $lists = DB::table('users')->get()->toArray();
        return view('lists',['users' => $lists]);
    }
    public function delete(Request $request){
        $a = $request->input('name');
        if (DB::table('users')->where('name', '=',$a )->delete()){
            echo '删除成功';
            header('Refresh:1;url=/list');
        }
    }
    public function insert( Request $request){
        $name = $request->input('name','');
        $password = $request->input('password','');
        if (!empty($name)){
            if (!empty($password)){
                $d = DB::table('users')->insert(['name'=>$name,'password'=>$password]);
                if ($d){
                    echo '注册成功';
                    header('Refresh:1;url=/index');
                }else{
                    echo '注册失败';
                }
            }else{
                echo '请输入密码';
            }
        }else{
            echo '请输入用户名';
        }
        $this->register();
    }
    public function register(){
        return view('register');
    }
    public function updates(Request $request){
        $name = $request->input('name','');
        dump($name);
        $password = $request->input('password','');
        if(DB::table('users')->where('name',$name)->update(['password'=>$password])){
            echo '修改成功';
        }

    }
    public function update(Request $request){
        return view('update',['name'=>$request->input('name')]);
    }

}
